const express=require("express");
const app=express();
const dao=require('./model/feng.js');
// 配置socket.io
var http = require('http').Server(app);
var io = require('socket.io')(http);

const cookieParser=require('cookie-parser');
//配置cookie
app.use(cookieParser());

//id专门转换
const ObjectId = require('mongodb').ObjectID;

const ejs=require('ejs');
app.set("view engine","ejs");

app.use('/public',express.static('./public/'));


//登陆页面
app.use('/login',(req,res)=>{
    res.render('login')
})
//chat页面
app.use('/chat',(req,res)=>{
    res.render('chat',{userss:req.cookies.username})
    //res.send(req.cookies.username)
})

//聊天室页面
app.use("/conversation",(req,res)=>{
    res.render("conversation",{userss:req.cookies.username})
})
//注册数据
app.use('/submit',(req,res)=>{
    let obj={
        Nickname:req.query.Nickname,
        pas:req.query.pas,
        // psd:req.query.psd
    }
    dao.insert("boke","userName",obj,(a)=>{
           res.send(a)
    })
})
//登陆数据
app.use('/logins',(req,res)=>{
    let obj={
        Nickname:req.query.user,
        pas:req.query.pasd,
    }
if(req.cookies.username){
    var usersName=req.query.user;
}
    // var usersName=req.query.user;
    res.cookie("username",usersName,{maxAge:1000*60*60*24,httpOnly:true})
console.log(res);
    dao.find("boke","userName",obj,(a)=>{
        
        if(req.cookies._id){
            var newid=a[0]._id;
        }
        res.cookie("_id",newid,{maxAge:1000*60*60*24,httpOnly:true})
        

        res.send(a)
    })
})

//chat-main 发布博客的数据
app.use("/mainChat",(req,res)=>{
    
    let obj={
        fie:req.query.fie,
    }
   dao.insert("mainboke","pullboke",obj,(a)=>{
        res.send(a);
   })
   
})
//获取页面数据
app.use('/daes',(req,res)=>{
    let obj={}
    dao.find("mainboke","pullboke",obj,(a)=>{
        console.log(a);
        res.send(a);
    })
})
///删除
app.use("/dels",(req,res)=>{
    let obj={
        _id:new ObjectId(req.query._id)
    };

    dao.del("mainboke","pullboke",obj,(a)=>{
       // console.log(a);
        res.send(a)
    })
})
//分页
app.use("/daea",(req,res)=>{
    let obj={};
	if(req.query.num==-1){
		var skip=0;
		var limit=0;
	}else{
		var skip=(req.query.num-1)*2;
		var limit=2;
	}
    dao.search("mainboke","pullboke",obj,skip,limit,(a)=>{
        
        res.send(a)
    })
})

// //pinglun
app.use('/pinglun',(req,res)=>{
    let obj={
        wenzi:req.query.wenzi,
    }
    dao.insert("mainboke","pinglun",obj,(a)=>{
        res.send(a)
    })
})
//// 配置长连接 connection事件，实时监听客户端请求
io.on('connection',(socket)=>{
	// chat接收客户端数据，on监听数据接收  chat可以自己命名，需要和前台保持一致
	socket.on('chats',(msg)=>{
		// 发送数据给客户端 io.emit('数据和前端保持一致')
		io.emit('send',msg)
	})
})

app.use("/logout",(req,res)=>{
   var userss="";
    res.cookie("username",userss,{maxAge:1000*60*60*24,httpOnly:true})
    res.send({userss:req.cookies.username})
})
//注销
app.use("/destro",(req,res)=>{
    var obj={
        _id:ObjectId(req.cookies._id)
    }
    console.log(obj);
    dao.del("boke","userName",obj,(a)=>{
        res.send(a)
    })
})

//注册页面
app.use('/',(req,res)=>{
    res.render('resiger')
})
http.listen('8989',"192.168.1.7")